package com.kabuda.hot100.二叉树.easy;

import com.kabuda.hot100.二叉树.TreeNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kabuda
 * @date 2025-03-06 22:02
 * @description 给定一个二叉树的根节点 root ，返回 它的 中序 遍历 。
 */
public class t94_二叉树的中序遍历 {
    public static void main(String[] args) {
        TreeNode treeNode1 = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(2);
        TreeNode treeNode3 = new TreeNode(3);
        treeNode1.right = treeNode2;
        treeNode2.left = treeNode3;
        System.out.println(inorderTraversal(treeNode1));
    }

    public static List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> result = new ArrayList<>();
        inorderTraversal(root, result);
        return result;
    }

    public static void inorderTraversal(TreeNode root, List<Integer> result) {
        if(root==null){
            return;
        }
        if (root.left != null) {
            inorderTraversal(root.left, result);
        }

        result.add(root.val);

        if (root.right != null) {
            inorderTraversal(root.right, result);
        }

    }
}
